package com.app.qbake.module.mine.adapter.delegate;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.qbake.R;
import com.app.qbake.module.mine.layout.GetEditGoodsLayout;
import com.app.qbake.module.mine.layout.GoodsDelLayout;
import com.app.qbake.module.mine.layout.GoodsUpDownLayout;
import com.app.qbake.module.publish.activity.PublishGoodsActivity;
import com.wind.base.adapter.BaseAdapterDelegate;
import com.wind.base.adapter.DisplayItem;
import com.wind.base.utils.ViewInflater;
import com.wind.data.mine.GetEditGoodsResponse;
import com.wind.data.square.bean.Goods;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyShelveGoodsDelegate extends BaseAdapterDelegate<MyShelveGoodsDelegate.ViewHolder> {


    private GoodsUpDownLayout mGoodsUpDownLayout;
    private GoodsDelLayout mGoodsDelLayout;
    private GetEditGoodsLayout mGetEditGoodsLayout;
    public MyShelveGoodsDelegate(Activity activity, int layoutRes) {
        super(activity, layoutRes);
        mGoodsUpDownLayout=new GoodsUpDownLayout(activity);
        mGoodsDelLayout=new GoodsDelLayout(activity);
        mGetEditGoodsLayout=new GetEditGoodsLayout(mActivity, new GetEditGoodsLayout.OnGetEditGoodsListener() {
            @Override
            public void onGetEditGoods(GetEditGoodsResponse response) {
                PublishGoodsActivity.start(mActivity, response.getData());
            }
        });
    }

    @Override
    protected ViewHolder onCreateViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    protected boolean isForViewType(@NonNull List<DisplayItem> items, int position) {
        return true;
    }

    @Override
    protected void onBindViewHolder(@NonNull List<DisplayItem> items, int position, @NonNull RecyclerView.ViewHolder holder, @NonNull List<Object> payloads) {
        final Goods goods = (Goods) items.get(position);
        ViewHolder vh = (ViewHolder) holder;
        ViewInflater.inflateImage(vh.iv, goods.getImage_url());
        ViewInflater.inflateText(vh.tv_desc, goods.getTitle());
        ViewInflater.inflateText(vh.tv_price, goods.getPrice());


        vh.tv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               mGetEditGoodsLayout.get(goods.getId());
            }
        });
        vh.tv_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mGoodsUpDownLayout.up(goods.getId());
            }
        });
        vh.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mGoodsDelLayout.del(goods.getId());
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.iv)
        ImageView iv;
        @BindView(R.id.tv_desc)
        TextView tv_desc;
        @BindView(R.id.tv_status)
        TextView tv_status;
        @BindView(R.id.tv_price)
        TextView tv_price;
        @BindView(R.id.tv_edit)
        TextView tv_edit;
        @BindView(R.id.tv_up)
        TextView tv_up;
        @BindView(R.id.tv_del)
        TextView tv_del;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
